package chuangyi.com.org.ViewModel;

import android.app.Activity;
import android.content.Context;
import android.databinding.Bindable;
import android.view.View;
import android.widget.Toast;

import com.solidfire.gson.internal.LinkedTreeMap;

import java.util.ArrayList;
import java.util.List;

import chuangyi.com.org.DOMIHome.BR;
import chuangyi.com.org.DOMIHome.Fragments.CommonFragment;
import chuangyi.com.org.DOMIHome.HomeActivity;
import chuangyi.com.org.DOMIHome.NotifyParentCheckedChanged;
import chuangyi.com.org.DOMIHome.R;
import chuangyi.com.org.DOMIHome.Utils.Utils;
import chuangyi.com.org.DOMIHome.Utils.WebApi;
import chuangyi.com.org.DOMIHome.adapter.ListBindingAdapter;
import chuangyi.com.org.DOMIHome.adapter.XListAdapterInterface;
import chuangyi.com.org.DOMIHome.adapter.XListBindingAdapter;

/**
 * Created by zxh on 16/12/11.
 */

public class RankModel extends XListModel implements NotifyParentCheckedChanged {
    private ListBindingAdapter<CheckedMenuItem> rankType;
    private int selectedType;
    private String currentType;
    private boolean loading;

    @Bindable
    public ListBindingAdapter<CheckedMenuItem> getRankType() {
        return rankType;
    }

//    @Bindable
//    public int getSelectedType() {
//        return selectedType;
//    }
//
//    public void setSelectedType(int selectedType) {
//        this.selectedType = selectedType;
//        setCurrentPage(1);
//        load(1);
//        notifyPropertyChanged(BR.selectedType);
//    }

    public RankModel(Context context) {
        super(context);
        super.getHandler().setTitle("搜索");
        getAdapter().setVariableId(BR.item);
        initRankType();
    }

    private void initRankType() {
        rankType = new ListBindingAdapter<CheckedMenuItem>(getContext(), BR.menu);
        CheckedMenuItem type = new CheckedMenuItem(getContext(), R.layout.checked_menu);
        type.setTitle("视频");
        type.setCode("v");
        type.setChecked(true);
        type.setParent(this);
        rankType.add(type);
        currentType = "v";

        type = new CheckedMenuItem(getContext(), R.layout.checked_menu);
        type.setTitle("文章");
        type.setCode("w");
        type.setChecked(false);
        type.setParent(this);
        rankType.add(type);

        type = new CheckedMenuItem(getContext(), R.layout.checked_menu);
        type.setTitle("专家");
        type.setCode("e");
        type.setChecked(false);
        type.setParent(this);
        rankType.add(type);

        type = new CheckedMenuItem(getContext(), R.layout.checked_menu);
        type.setTitle("董秘");
        type.setCode("董秘");
        type.setChecked(false);
        type.setParent(this);
        rankType.add(type);

        type = new CheckedMenuItem(getContext(), R.layout.checked_menu);
        type.setTitle("新人");
        type.setCode("新人");
        type.setChecked(false);
        type.setParent(this);
        rankType.add(type);
    }

    /**
     * 点击规则按钮，显示排行榜规则
     *
     * @param view
     */
    public void onClickRankRule(View view) {
        CommonFragment fragment = new CommonFragment();
        fragment.setLayoutId(R.layout.f_rank_rule);
        ObservableWithGoBackHandler handler = new ObservableWithGoBackHandler(getContext());
        handler.getHandler().setTitle("排行规则");
        fragment.setVariableId(BR.handler);
        fragment.setDataModel(handler);
        HomeActivity.switchFragment((Activity) getContext(), fragment);
    }

    @Override
    public void notifyCheckedChanged(CheckedMenuItem menu) {
        if (menu.isChecked()) {
            if (currentType == null || !currentType.equals(menu.getCode()))
                currentType = menu.getCode();
            getAdapter().clear(true);
            setCurrentPage(1);
            load(1);
        }
    }

    @Override
    public void load(final int page) {
        if (loading) {
            return;
        }
        loading = true;
        setNoMore();
        final XListAdapterInterface adapter = getAdapter();
        adapter.clear(true);
        notifyDataSetChanged(adapter);
        new Thread(new Runnable() {
            @Override
            public void run() {
                final ArrayList list = WebApi.getRank(getContext(), currentType);
                try {
                    for (Object item : list) {
                        LinkedTreeMap map = (LinkedTreeMap) item;
                        RankItem rankItem = new RankItem(getContext(), R.layout.i_rank);
/*视频 rowno:排名，videoid：视频id，videoname：视频名称，cover：封面，videopath：视频地址，nickname：上传会员昵称，play：播放次数，
     文章 rowno:排名，articleid:文章id,title:文章标题,lable:文章简介，nickname：上传会员昵称，click：点击次数，
     专家 rowno：排名，uid：用户id，nickname：会员昵称，avatar：头像，realname：真实姓名，expertdomain：专家领域，
     董秘 rowno：排名，uid：用户id，nickname：会员昵称，avatar：头像，realname：真实姓名，expertdomain：领域*/
                        rankItem.setType(currentType);

                        switch (currentType) {
//"rowno":"2","articleid":"2","title":"测试已","lable":"1,3,2","nickname":"admin","click":"2"},{"rowno":"1","articleid":"1","title":"测试 ","lable":"1,2","nickname":"15022189533","click":"1"
                            case "w":
                                rankItem.setNo(Utils.getIntValue(map.get("rowno")));
                                rankItem.setId(Utils.getIntValue(map.get("articleid")));
                                rankItem.setTitle((String) map.get("title"));
                                rankItem.setImg((String) map.get("avatar"));
                                rankItem.setDesc((String) map.get("content"));
                                rankItem.setNickName((String) map.get("nickname"));
                                rankItem.setHotDegree(Utils.getIntValue(map.get("click")));
                                break;
                            case "v":
//{"rowno":"1","videoid":"1","videoname":"视频1","videodesc":"视频介绍视频介绍","cover":"","videopath":"","nickname":"18622536910","play":"0"}
                                rankItem.setNo(Utils.getIntValue(map.get("rowno")));
                                rankItem.setId(Utils.getIntValue(map.get("videoid")));
                                rankItem.setTitle((String) map.get("videoname"));
                                rankItem.setImg((String) map.get("cover"));
                                rankItem.setDesc((String) map.get("videodesc"));
                                rankItem.setNickName((String) map.get("nickname"));
                                rankItem.setHotDegree(Utils.getIntValue(map.get("play")));
                                break;
                            case "e":
                            case "董秘":
                            case "新人":
//{"rowno":"1","uid":"1","nickname":"admin","avatar":"null","realname":"null","expertdomain":"股权,法务","industry":"电子商务","company":"","click":"2","doclike":"0","expertask":"8"}
//{"rowno":"1","uid":"26","nickname":"nul****","avatar":"","realname":"null","expertdomain":null,"click":"0","doclike":"0","ucomment":"0","expertask":"0"}
                                rankItem.setNo(Utils.getIntValue(map.get("rowno")));
                                rankItem.setId(Utils.getIntValue(map.get("uid")));
                                rankItem.setNickName((String) map.get("nickname"));
                                rankItem.setImg((String) map.get("avatar"));
                                rankItem.setRealName((String) map.get("realname"));
                                rankItem.setDesc((String) map.get("expertdomain"));
                                rankItem.setTitle((String) map.get("nickname"));
                                rankItem.setHotDegree(Utils.getIntValue(map.get("click")));
                                break;
                        }
                        adapter.add(rankItem);
                        notifyDataSetChanged(list);
                    }
                    if (list.size() > 0) {
                        setCurrentPage(page);
                    }
                } catch (Exception ex) {
                    showToast("解析数据时发生错误");
                }
                loading = false;
            }
        }).start();
    }
}
